
**********
* Readme *
**********

* This script generates Figure 1: Occupations and labor income.


* Root folder (PATH TO BE DEFINED BY THE USER)
**********************************************
clear all
global analysis "C:/***/replication_package"


* Timestamped log
*****************
global today = strofreal(date(c(current_date), "DMY"), "%tdYYNNDD")
log using "${analysis}/code/logs/3_1_descriptive_plot_${today}.smcl", replace


************************************************************************************
*   Stacked percent of net labor income by occupation (adult urban employed pop)   *
************************************************************************************
use "${analysis}/data/2_2_pof_clean.dta", clear

drop if work_state_name == "Inactive"
gen fweight = round(pweight)
keep winc work_state fweight

recode winc ///
  (     1 /   125 = 0.5 ) /// 
  (   125 /   250 = 1.5 ) /// 
  (   250 /   500 = 2.5 ) /// 
  (   500 /  1000 = 3.5 ) /// 
  (  1000 /  2000 = 4.5 ) /// 
  (  2000 /  4000 = 5.5 ) /// 
  (  4000 /  8000 = 6.5 ) /// 
  (  8000 / 16000 = 7.5 ), gen(inc_group)

tab inc_group work_state [fweight = fweight]

gen n_ = 1/10^6
collapse (sum) n_ [fweight = fweight], by(inc_group work_state)

reshape wide n_, i(inc_group) j(work_state)
rename n_1 n_oaw
rename n_2 n_ee
rename n_3 n_er

gen n_total = n_oaw + n_ee + n_er
gen share_oaw = n_oaw / n_total 
gen share_ee  = n_ee  / n_total 
gen share_er  = n_er  / n_total 

gen cum_share_oaw = (share_oaw) * 100
gen cum_share_ee  = (share_oaw + share_ee) * 100
gen cum_share_er  = (share_oaw + share_ee + share_er) * 100

label var cum_share_oaw "Own-account workers"
label var cum_share_ee  "Employees"
label var cum_share_er  "Employers"


* FIGURE 1
**********

graph twoway ///
  (scatteri -44 0  24 8, mc(white) yaxis(2)) ///
  (scatteri   0 0 180 8, mc(white) yaxis(1))

addplot: ///
  (bar n_total inc_group,       lcolor(white) color(gs4)    base(0) yaxis(2)) ///
  (bar cum_share_er  inc_group, lcolor(white) color(gs13)    base(0) yaxis(1)) ///
  (bar cum_share_ee  inc_group, lcolor(white) color(gs10)   base(0) yaxis(1)) ///
  (bar cum_share_oaw inc_group, lcolor(white) color(gs7)   base(0) yaxis(1)), ///
  ytitle("", axis(1)) ///
  ytitle("{bf:Employed individuals     }" "{bf:by labor income     }" "{bf:(in millions)     }", placement(n) orient(horizontal) j(right) axis(2) linegap(.75)) ///
  xtitle("{bf:Net labor income (in R{c $|}, log scale)}") ///
  text(104 9.8 "{bf:Breakdown of}" "{bf:employment status}" "{bf:by labor income}", place(se) just(left) linegap(.75)) ///
  ylabel(0 "0%" 20 "20%" 40 "40%" 60 "60%" 80 "80%" 100 "100%", format(%9.0f) axis(1)) ///
  ylabel(0 (5) 30, format(%9.0f) grid axis(2)) ///
  xlabel(0 "min" 1 "125" 2 "250" 3 "500" 4 "1k" 5 "2k" 6 "4k" 7 "8k" 8 "max") ///
  legend(order(4 5 6) pos(4) ring(1)) graphregion(margin(l-8 r-9))
  
gr_edit yaxis2.title.DragBy -2 7
gr_edit .legend.DragBy -2 -7

graph export "${analysis}/results/figures/fig1.eps", replace


* End of script
***************
cap log close